package Controller

import (
	"computer_set/global"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"time"
)

// 在Controller/controller.go中修改GetOriginPieChart函数
func GetOriginPieChart(c *gin.Context) {
	// 从数据库直接统计原始产地
	var originStats []struct {
		Origin string
		Count  int
	}

	db := global.DB
	err := db.Raw(`
        SELECT origin, COUNT(*) as count 
        FROM bencaogangmu 
        WHERE origin != ''
        GROUP BY origin
        ORDER BY count DESC
    `).Scan(&originStats).Error

	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"error": "数据统计失败: " + err.Error(),
		})
		return
	}
	// 直接转换为饼图格式
	pieData := make([]gin.H, 0)
	for _, stat := range originStats {
		pieData = append(pieData, gin.H{
			"name":  stat.Origin,
			"value": stat.Count,
		})
	}
	c.JSON(http.StatusOK, pieData)
}

type FakeYieldData struct {
	Year      int             `json:"year"`
	Provinces []ProvinceYield `json:"provinces"`
}

type ProvinceYield struct {
	Province string `json:"province"`
	Yield    int    `json:"yield"` // 单位：吨
}

func generateFakeYieldData() []FakeYieldData {
	//provinces := []string{
	//	"云南", "四川", "贵州", "甘肃", "广西",
	//	"西藏", "青海", "湖北", "湖南", "陕西",
	//	"河南", "河北", "山西", "内蒙古", "吉林",
	//}

	baseYield := map[string]int{
		"云南": 51234, "四川": 45123, "贵州": 31931,
		"甘肃": 25233, "广西": 22331, "西藏": 18334,
		"青海": 15413, "湖北": 28000, "湖南": 27000,
		"陕西": 23027, "河南": 20645, "河北": 18042,
		"山西": 16023, "内蒙古": 12032, "吉林": 10424,
	}

	// 定义特殊年份的增减规则（完全手动控制）
	specialRules := map[int]map[string]float64{
		2010: {
			"云南": 1.25, // +25%
			"四川": 0.85, // -15%
		},
		2015: {
			"贵州": 1.4, // +40%
			"甘肃": 0.7, // -30%
		},
		2020: {
			"湖北": 1.35, // +35%（模拟疫情后增长）
		},
	}

	var data []FakeYieldData
	currentYear := time.Now().Year() - 1

	for year := 2000; year <= currentYear; year++ {
		yearlyData := make([]ProvinceYield, 0)

		for province, base := range baseYield {
			yield := base

			// 应用特殊年份规则
			if rules, ok := specialRules[year]; ok {
				if ratio, ok := rules[province]; ok {
					yield = int(float64(base) * ratio)
					yearlyData = append(yearlyData, ProvinceYield{
						Province: province,
						Yield:    yield,
					})
					continue
				}
			}
			var growth float64
			if year%2 == 0 {
				growth = 1.0 + float64(year-2000)*0.01
				yield = int(float64(base) * growth)
			} else {
				growth = 1.0 - float64(year-2000)*0.02
				yield = int(float64(base) * growth)
			}
			growth = 1.0 + float64(year-2000)*0.015
			yield = int(float64(base) * growth)

			yearlyData = append(yearlyData, ProvinceYield{
				Province: province,
				Yield:    yield,
			})
		}

		data = append(data, FakeYieldData{
			Year:      year,
			Provinces: yearlyData,
		})
	}

	return data
}

func GetFakeYieldData(c *gin.Context) {
	rand.Seed(1234)

	// 生成数据
	data := generateFakeYieldData()

	// 支持查询参数过滤
	if province := c.Query("province"); province != "" {
		filtered := make([]FakeYieldData, 0)
		for _, yearData := range data {
			for _, p := range yearData.Provinces {
				if p.Province == province {
					filtered = append(filtered, FakeYieldData{
						Year:      yearData.Year,
						Provinces: []ProvinceYield{p},
					})
				}
			}
		}
		c.JSON(200, filtered)
		return
	}

	c.JSON(200, data)
}
